home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / src / route.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  1.9 KB  |  90 lines

  1. /*
  2.  *  libnet
  3.  *  route.c - Utility routines for doing routing socket queries.
  4.  *  This is pilfered from UNP.
  5.  */
  6.  
  7. #if (HAVE_CONFIG_H)
  8. #include "../include/config.h"
  9. #endif
  10. #include "../include/libnet.h"
  11.  
  12. #if (RAW_IS_COOKED)
  13. #include <net/route.h>
  14.  
  15. /* 2.6 and higher have the routing sockets */
  16.  
  17. #if (STUPID_SOLARIS_CHECKSUM_BUG) /* It's 2.5.1 or lower. */
  18. u_long
  19. libnet_get_next_hop(u_long ip, char *ebuf)
  20. {    
  21.     return -1;
  22. }
  23. #else /* It's 2.6 or higher. */
  24. u_long
  25. libnet_get_next_hop(u_long ip, char *ebuf)
  26. {    
  27.     int fd,n;
  28.     char buf[4096];
  29.     struct rt_msghdr *rtm;
  30.     struct sockaddr_in *sin;
  31.     pid_t pid;
  32.     u_long seq;
  33.  
  34.     if ((fd=socket(PF_ROUTE, SOCK_RAW, 0))==-1)
  35.     {
  36.         sprintf( ebuf, "socket: %s", strerror(errno));
  37.         return -1;
  38.     }
  39.  
  40.     memset(buf,0,4096);
  41.     rtm=(struct rt_msghdr *)buf;
  42.  
  43.     rtm->rtm_msglen=sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
  44.     rtm->rtm_version=RTM_VERSION;
  45.     rtm->rtm_type=RTM_GET;
  46.     rtm->rtm_addrs=RTA_DST;
  47.     rtm->rtm_pid=pid=getpid();
  48.     rtm->rtm_seq=seq=libnet_get_prand(PRu32);
  49.  
  50.     sin=(struct sockaddr_in *)(rtm+1);
  51.     sin->sin_family=AF_INET;
  52.     sin->sin_addr.s_addr=ip;
  53.  
  54.     n=write(fd, rtm, rtm->rtm_msglen);
  55.     if (n<0)
  56.     {
  57.         sprintf( ebuf, "write: %s", strerror(errno));
  58.         close(fd); 
  59.         return -1;
  60.     }
  61.     if (n!=rtm->rtm_msglen)
  62.     {
  63.         sprintf( ebuf, "write: couldn't write full message");
  64.         close(fd); 
  65.         return -1;
  66.     }
  67.   
  68.     do
  69.     {
  70.         n=read(fd, rtm, sizeof buf); 
  71.     } while (rtm->rtm_type != RTM_GET || rtm->rtm_seq != seq || 
  72.              rtm->rtm_pid != pid);
  73.  
  74.     rtm=(struct rt_msghdr *)buf;
  75.  
  76.     if (!(rtm->rtm_addrs & 2))
  77.     {
  78.         sprintf( ebuf, "can't retrieve gateway");
  79.         close(fd); 
  80.         return -1;
  81.     }
  82.  
  83.     sin=(struct sockaddr_in *)(((struct sockaddr_in *)(rtm+1))+1);
  84.     close(fd);
  85.     return sin->sin_addr.s_addr;
  86. }
  87. #endif /* STUPID_SOLARIS_CHECKSUM_BUG */
  88. #endif /* RAW_IS_COOKED */
  89. /* EOF */
  90.